package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/setmeal")
@ApiOperation("套餐相关接口")
@Slf4j

public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }
    @GetMapping(value ="/page")
    @ApiOperation("套餐分页查询")
    //注意：query查询不需要requestBody
    public Result<PageResult> page(SetmealPageQueryDTO dishPageQueryDTO){
        PageResult pageResult=setmealService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态")
    //因为修改套餐状态，所以要清空缓存（所有）
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result status(@PathVariable int status,@RequestParam long id){
        log.info("修改套餐状态为{}",status);
        setmealService.updateDishStatus(status,id);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result ids(@RequestParam(value = "ids") Long[] ids){

        setmealService.deleteBataches(ids);
        log.info("删除套餐{}",ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@RequestParam  Long id){

        return Result.success(setmealService.getById(id));

    }

}
